package com.fit.hibernate;

import java.io.Serializable;
import java.util.Collection;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;


import com.fit.beans.UserBean;

import com.fit.interfaces.User;
import com.fit.service.Users;


public class UsersServiceHibernate implements Users {

	private Session session;
	

	
	public UsersServiceHibernate(Session session) {
		
		this.session = session;
	}

	@Override
	public User save(User user) {
		// TODO Auto-generated method stub
		return (User) session.get(UserBean.class, session.save(user));
	}

	@Override
	public Collection<User> retrive() {
		Criteria criteria = session.createCriteria(UserBean.class);
		return criteria.list();
	}

	@Override
	public User retriveSingleUser(String userName) {
		
//		if(userName.equalsIgnoreCase("admin")){
////			session.save(AppModule.context.getBean("admin"));
////			
//			Criteria tmp =  session.createCriteria(UserBean.class).add(Restrictions.eq("userName", "admin"));
////			Criteria tmp = session.createCriteria(UserBean.class).add(Restrictions.eq("userName", userName));
//			return (User) tmp.uniqueResult();
//		}else{
//		
		Criteria criteria = session.createCriteria(UserBean.class).add(Restrictions.eq("userName", userName));
		
		
		
		return (User) criteria.uniqueResult();
		//}
	}


	
	public User retriveSingleUser(Serializable id) {
		
		return (User) session.get(UserBean.class, id);
	}

	@Override
	public User editSingleUser(User user) {
		// TODO Auto-generated method stub
		return (User) session.merge(user);
	}

	

}
